package com.piotrs10.bsk.gui.views;

import java.util.ArrayList;
import java.util.Iterator;

import com.piotrs10.bsk.dao.PracownikDao;
import com.piotrs10.bsk.dao.SamochodDao;
import com.piotrs10.bsk.dao.SprzedazDao;
import com.piotrs10.bsk.dao.TypSamochoduDao;
import com.piotrs10.bsk.model.Pracownik;
import com.piotrs10.bsk.model.Samochod;
import com.piotrs10.bsk.model.Sprzedaz;
import com.piotrs10.bsk.model.TypSamochodu;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

public class ModifySamochod extends ModifyLayout {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4458024953188277985L;

	private TextField markaField, modelField, cenaField, rokField;
	private ComboBox sprzedazBox, typBox;
	private boolean insert;
	private Integer id;
	
	public ModifySamochod(boolean isInsert, Integer id, Window modalWindow) {
		super(isInsert, modalWindow);
		this.insert = isInsert;
		this.id = id;
		this.markaField = new TextField("Marka");
		this.modelField = new TextField("Model");
		this.cenaField = new TextField("Cena");
		this.rokField = new TextField("Rok produkcji");
		this.sprzedazBox = new ComboBox("Sprzedaz");
		this.typBox = new ComboBox("Typ");
		this.commitButton = new Button("Zapisz");

		this.loadFields(isInsert, id);

		this.fieldsPanel.addComponent(this.markaField);
		this.fieldsPanel.addComponent(this.modelField);
		this.fieldsPanel.addComponent(this.cenaField);
		this.fieldsPanel.addComponent(this.rokField);
		this.fieldsPanel.addComponent(this.sprzedazBox);
		this.fieldsPanel.addComponent(this.typBox);
		this.fieldsPanel.addComponent(this.commitButton);
		this.commitButton.addListener(this);
	}

	private void loadFields(boolean insert, Integer id) {
		// TODO: Pobrac z bazy danych co trzeba
		ArrayList<TypSamochodu> typesList = new TypSamochoduDao().getAll();
		for(TypSamochodu current : typesList) {
			this.typBox.addItem(current.getId());
		}
		
		ArrayList<Sprzedaz> salesList = new SprzedazDao().getAll();
		for(Sprzedaz current : salesList) {
			this.sprzedazBox.addItem(current.getId());
		}
		
		if(insert == false) {
			Samochod samochod = new SamochodDao().get(id);
			this.markaField.setValue(samochod.getMarka());
			this.modelField.setValue(samochod.getModel());
			this.cenaField.setValue(samochod.getCena());
			this.rokField.setValue(samochod.getRok());

			Iterator<?> iterator = this.sprzedazBox.getItemIds().iterator();
			Object item = null;
			int typId = 0;
			do {
				item = iterator.next();
				typId = Integer.valueOf(this.sprzedazBox.getItemCaption(item));
			} while(typId != samochod.getSprzedaz_id());
			this.sprzedazBox.setValue(item);
			
			iterator = this.typBox.getItemIds().iterator();
			item = null;
			int sprId = 0;
			do {
				item = iterator.next();
				sprId = Integer.valueOf(this.typBox.getItemCaption(item));
			} while(sprId != samochod.getTyp());
			this.typBox.setValue(item);
		}
	}

	@Override
	public void buttonClick(ClickEvent event) {
		// TODO Auto-generated method stub
		Samochod s = new Samochod();
		s.setMarka(this.markaField.getValue().toString());
		s.setModel(this.modelField.getValue().toString());
		s.setCena(Float.valueOf(this.cenaField.getValue().toString()));
		s.setRok(Integer.valueOf(this.rokField.getValue().toString()));
		s.setSprzedaz_id(Integer.valueOf(this.sprzedazBox.getValue().toString()));
		s.setTyp(Integer.valueOf(this.typBox.getValue().toString()));
		if(insert == false)
			s.setId(this.id);
		new SamochodDao().save(this.insert, s);
		this.modalWindow.getParent().removeWindow(this.modalWindow);
	}

	public static void fillTable(Table table) {
		table.addContainerProperty("ID", Integer.class, null);
		table.addContainerProperty("Marka", String.class, null);
		table.addContainerProperty("Model", String.class, null);
		table.addContainerProperty("Cena", Float.class, null);
		table.addContainerProperty("Rok", Integer.class, null);
		table.addContainerProperty("Sprzedaz", Integer.class, null);
		table.addContainerProperty("Typ", Integer.class, null);
		
		ArrayList<Samochod> list = new SamochodDao().getAll();
		for (int i = 0; i < list.size(); i++) {
			Samochod s = list.get(i);
			table.addItem(new Object[] {s.getId(), s.getMarka(), s.getModel(), s.getCena(),
					s.getRok(), s.getSprzedaz_id(), s.getTyp()}, i);
		}
	}

	public String getWinWidth() {
		return "250px";
	}

	public String getWinHeight() {
		return "350px";
	}
	
	public static void refresh(Table table) {
		table.removeAllItems();
		ArrayList<Samochod> list = new SamochodDao().getAll();
		for (int j = 0; j < list.size(); j++) {
			Samochod s = list.get(j);
			table.addItem(new Object[] {s.getId(), s.getMarka(), s.getModel(), s.getCena(),
					s.getRok(), s.getSprzedaz_id(), s.getTyp()}, j);
		}
	}
}
